package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysRoleAccountRel;
import com.freemalll.system.service.ISysRoleAccountRelService;

/**
 * 角色账户关系 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/roleAccountRel")
public class SysRoleAccountRelController extends BaseController
{
    @Autowired
    private ISysRoleAccountRelService roleAccountRelService;

    /**
     * 获取角色账户关系列表
     */
    @RequiresPermissions("system:roleAccountRel:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleAccountRel roleAccountRel)
    {
        startPage();
        List<SysRoleAccountRel> list = roleAccountRelService.selectRoleAccountRelList(roleAccountRel);
        return getDataTable(list);
    }

    @Log(title = "角色账户关系", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:roleAccountRel:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleAccountRel roleAccountRel)
    {
        List<SysRoleAccountRel> list = roleAccountRelService.selectRoleAccountRelList(roleAccountRel);
        ExcelUtil<SysRoleAccountRel> util = new ExcelUtil<SysRoleAccountRel>(SysRoleAccountRel.class);
        util.exportExcel(response, list, "角色账户关系数据");
    }

    /**
     * 根据角色账户关系编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(roleAccountRelService.selectRoleAccountRelById(id));
    }

    /**
     * 根据账户id查询关系列表
     */
    @GetMapping(value = "/accountId/{accountId}")
    public AjaxResult getInfoByAccountId(@PathVariable String accountId)
    {
        return success(roleAccountRelService.selectRoleAccountRelByAccountId(accountId));
    }

    /**
     * 根据角色id查询关系列表
     */
    @GetMapping(value = "/roleId/{roleId}")
    public AjaxResult getInfoByRoleId(@PathVariable String roleId)
    {
        return success(roleAccountRelService.selectRoleAccountRelByRoleId(roleId));
    }

    /**
     * 新增角色账户关系
     */
    @RequiresPermissions("system:roleAccountRel:add")
    @Log(title = "角色账户关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRoleAccountRel roleAccountRel)
    {
        roleAccountRel.setCreateName(SecurityUtils.getUsername());
        return toAjax(roleAccountRelService.insertRoleAccountRel(roleAccountRel));
    }

    /**
     * 修改角色账户关系
     */
    @RequiresPermissions("system:roleAccountRel:edit")
    @Log(title = "角色账户关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysRoleAccountRel roleAccountRel)
    {
        roleAccountRel.setUpdateName(SecurityUtils.getUsername());
        return toAjax(roleAccountRelService.updateRoleAccountRel(roleAccountRel));
    }

    /**
     * 删除角色账户关系
     */
    @RequiresPermissions("system:roleAccountRel:remove")
    @Log(title = "角色账户关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        roleAccountRelService.deleteRoleAccountRelByIds(ids);
        return success();
    }
}